﻿using Microsoft.Xna.Framework;
using Kinected;

namespace TowerDefense
{
    /// <summary>A Tower fires a Projectile at an Enemy.</summary>
    class Tower
    {
        private Circle bounds;
        private Vector2 origin;
        private float scale, compression, scale2, compression2, rotation;

        private int fireRate;
        private int lastFire;

        public float FlashAlpha;

        public float TowerScale { get { return scale - compression; } }

        public float FlashScale { get { return scale2 - compression2; } }

        public Vector2 Position { get { return bounds.Center - origin; } }

        public Vector2 Origin { get { return origin; } }

        public Circle Bounds { get { return bounds; } }

        public float Rotation { get { return rotation; } }
        
        /// <summary>A Tower fires a Projectile at an Enemy.</summary>
        public Tower(Circle circle, float towerTextureWidth, float flashTextureWidth)
        {
            this.bounds = circle;

            origin = new Vector2(circle.Radius);

            scale = circle.Radius / (towerTextureWidth / 2);
            scale2 = circle.Radius / (flashTextureWidth / 2);
            compression = scale * 0.5f;

            fireRate = 20;
            lastFire = 0;
        }

        /// <summary>The tower fired, so begin to animate it.</summary>
        public void Fire()
        {
            compression2 = scale2 * 0.5f;
        }

        /// <summary>Is the tower able to fire again?</summary>
        public bool FireProjectile()
        {
            lastFire++;
            lastFire %= fireRate;

            if (lastFire == 0)
            {
                FlashAlpha = 1.0f;
                return true;
            }

            return false;
        }

        /// <summary>Update the tower's animations.</summary>
        public void Update()
        {
            FlashAlpha = MathHelper.Lerp(FlashAlpha, 0.0f, 0.05f);

            rotation = MathHelper.WrapAngle(rotation + 0.01f);

            compression = MathHelper.Lerp(compression, 0.0f, 0.1f);
            compression2 = MathHelper.Lerp(compression2, 0.0f, 0.1f);
        }
    }
}
